<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\model\bill;

use app\common\model\BaseModel;
use app\common\model\property\basic\PropertyBasicModel;
use app\common\model\room\RoomBasicModel;
use app\common\model\energy\EnergyBillModel;
use app\common\model\users\UsersModel;

class BillCatalogModel extends BaseModel
{
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'add_time';
    protected $updateTime = false;

    public static function tablePk(): string
    {
        return 'id';
    }

    public static function tableName(): string
    {
        return 'bill_catalog';
    }

    public function propertyInfo()
    {
        return $this->hasOne(PropertyBasicModel::class, 'id', 'property_id');
    }

    public function cateInfo()
    {
        return $this->hasOne(BillCateModel::class, 'id', 'cate_id');
    }

    public function roomInfo()
    {
        return $this->hasOne(RoomBasicModel::class, 'id', 'room_id');
    }

    public function billBasicInfo()
    {
        return $this->hasOne(BillBasicModel::class, 'id', 'bill_id');
    }

    public function userInfo()
    {
        return $this->hasOne(UsersModel::class, 'id', 'user_id');
    }

    public function getUseNumZAttr()
    {
        $data = [];
        $data = EnergyBillModel::where('id', $this->energy_bill_id)
            ->value('use_num_z');
        return $data;
    }

    public function getEnergyBillInfoAttr()
    {
        $data = [];
        $data = EnergyBillModel::where('id', $this->energy_bill_id)
            ->with([
                'energyMeterInfo' => function ($query) {
                    $query->bind(['energy_no' => 'energy_no']);
                }
            ])
            ->find();
        return $data;
    }

    public function getPlanRecordAttr($value)
    {
        try {
            return json_decode($value, true);
        } catch (\Exception $e) {
            return [];
        }
    }


    /**
     * 线上支付金额
     * @return float
     *
     * @author Amos
     * 2024-02-08 19:45
     */
    public function getOnlineAmountAttr()
    {
        return BillFinanceModel::where('catalog_id', $this->id)
            ->where('pay_type', 1)
            ->where('bill_id', $this->bill_id)
            ->sum('amount');
    }

    /**
     * 线下支付金额
     * @return float
     *
     * @author Amos
     * 2024-02-08 19:45
     */
    public function getOfflineAmountAttr()
    {
        return BillFinanceModel::where('catalog_id', $this->id)
            ->where('pay_type', 2)
            ->where('bill_id', $this->bill_id)
            ->sum('amount');
    }


    /**
     * 己收款金额
     * @return string
     *
     * @author Amos
     * 2024-02-08 19:45
     */
    public function getCheckPayAmountAttr()
    {
        # 己收款金额 = 线下支付金额 + 线上支付金额
        return bcadd($this->offline_amount, $this->online_Amount, 2);
    }

    /**
     * 待支付金额
     * @return string
     *
     * @author Amos
     * 2024-02-08 19:45
     */
    public function getStayPayAmountAttr()
    {
        # 待支付 =  账单总额 - 线下支付金额 - 线上支付金额
        if ($this->status == 2 || $this->status == 5 || $this->status == 8) {
            return bcsub(bcsub($this->total_amount, $this->offline_amount, 2), $this->online_Amount, 2);
        } else {
            return '--';
        }

    }

}
